import {
  getAppList,
  appSwitch,
  getAppListAll,
  getTenantList,
} from "../services/appListMenu";

const initData = {
  appListMenuData: [],
  appList: [],
  appBusinessObj: [],
  appListMenuDataAll: [],
  appListAll: [],
  appBusinessObjAll: [],
  focusApp: {},
  tenantList: [],
};

const formatAppList = (appListData = []) => {
  const appList = [];
  const appBusinessObj = {};
  appListData.forEach((item) => {
    const { id, name, business } = item;
    appList.push({ id, name });
    appBusinessObj[id] = business;
  });
  return { appList, appBusinessObj };
};

const formatTenantAppList = (appListData = []) => {
  const appList = [];
  const appBusinessObj = {};
  appListData.forEach((item) => {
    const { apps } = item;
    apps.forEach((i) => {
      const { id, appName: name, busi } = i;
      appList.push({ id, name });

      let newBusiness = [];

      if (busi) {
        newBusiness = busi.map((b) => {
          return { id: b.id, name: b.busiName, appId: b.appId };
        });
      }

      appBusinessObj[id] = newBusiness;
    });
  });
  return { appList, appBusinessObj };
};

export default {
  namespace: "appListMenu",
  state: {
    ...initData,
  },
  effects: {
    *getAppListData({ payload }, { call, put }) {
      const {
        datas: appListMenuData = [],
        errCode,
        errMsg,
        errortext,
      } = yield call(getAppList, payload);

      const { appList = [], appBusinessObj = [] } = formatTenantAppList(
        appListMenuData,
      );

      yield put({
        type: "get/appListMenu",
        payload: { appList, appBusinessObj, appListMenuData },
      });
      return {
        errCode,
        errMsg: errMsg || errortext,
        appList,
        appListMenuData,
        appBusinessObj,
      };
    },
    *getAppListAllData({ payload }, { call, put }) {
      const { datas = {}, errCode, errMsg, errortext } = yield call(
        getAppListAll,
        payload,
      );
      const appListMenuData = datas.app || [];
      const { appList = [], appBusinessObj = {} } = formatAppList(
        appListMenuData,
      );
      yield put({
        type: "get/getAppListAll",
        payload: { appList, appBusinessObj, appListMenuData },
      });
      return {
        errCode,
        errMsg: errMsg || errortext,
        datas,
        appList,
        appBusinessObj,
      };
    },
    *appSwitch({ payload }, { call }) {
      yield call(appSwitch, payload);
    },
    *getTenantList({ payload }, { call, put }) {
      const { datas = {}, errCode, errMsg, errortext } = yield call(
        getTenantList,
        payload,
      );
      yield put({
        type: "get/tenantList",
        payload: { tenantList: datas },
      });
      return {
        errCode,
        errMsg: errMsg || errortext,
        datas,
      };
    },
  },
  reducers: {
    focusApp: (state, { payload }) => {
      return {
        ...state,
        focusApp: payload,
      };
    },
    "get/getAppListAll": (state, { payload }) => {
      const {
        appList = [],
        appBusinessObj = {},
        appListMenuData = [],
      } = payload;
      const newAppList = appList;

      return {
        ...state,
        appListAll: appList,
        // appBusinessObjAll: appBusinessObj,
        appListMenuDataAll: appListMenuData,
        appList: newAppList,
        // appListMenuData: newAppListMenuData,
        appBusinessObj,
      };
    },
    "get/appListMenu": (state, { payload }) => {
      const {
        appList = [],
        appBusinessObj = [],
        appListMenuData = [],
      } = payload;

      return {
        ...state,
        appListMenuData,
        appList,
        appBusinessObj,
        appBusinessObjAll: appBusinessObj,
        appListAll: appList,
      };
    },
    "get/tenantList": (state, { payload }) => {
      const { tenantList = [] } = payload;
      return {
        ...state,
        tenantList,
      };
    },

    clearData: () => ({ ...initData }),
  },
};
